package com.atguigu.listener;

import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.http.HttpSessionAttributeListener;
import jakarta.servlet.http.HttpSessionBindingEvent;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;

@WebListener
public class MySessionListener implements HttpSessionListener, HttpSessionAttributeListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("Session"+se.getSession().hashCode()+"Created");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("Session"+se.getSession().hashCode()+"Destroyed");
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        System.out.println("Session"+se.getSession().hashCode()+"AttributeAdded:"+se.getName()+":"+se.getValue());
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        System.out.println("Session"+se.getSession().hashCode()+"AttributeRemoved:"+se.getName()+":"+se.getValue());
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent se) {
        System.out.println("Session"+se.getSession().hashCode()+
                "AttributeReplaced:"+se.getName()+":"+se.getValue()+
                " to:"+se.getSession().getAttribute(se.getName()));
    }
}
